﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Expression;
using System.Collections;

namespace Model
{
    public class AdvogadoPrestador
    {
        #region Atributos
        private string idPrestador;
        private string nome;
        private string oab;
        private string cpf;
        private string rg;
        private DateTime dataCadastro;
        private Usuario usuario;
        private Endereco endereco;
        private string email;
        private IList processos;

        #endregion

        #region Propriedades

        public string IdPrestador
        {
            get { return idPrestador; }
            set { idPrestador = value; }
        }
       

        public string Nome
        {
            get { return nome; }
            set { nome = value; }
        }
        

        public string Oab
        {
            get { return oab; }
            set { oab = value; }
        }
        

        public string Cpf
        {
            get { return cpf; }
            set { cpf = value; }
        }
        

        public string Rg
        {
            get { return rg; }
            set { rg = value; }
        }
       

        public DateTime DataCadastro
        {
            get { return dataCadastro; }
            set { dataCadastro = value; }
        }
      

        public Usuario Usuario
        {
            get { return usuario; }
            set { usuario = value; }
        }
       

        public Endereco Endereco
        {
            get { return endereco; }
            set { endereco = value; }
        }
       

        public string Email
        {
            get { return email; }
            set { email = value; }
        }


        public IList Processos
        {
            get { return processos; }
            set { processos = value; }
        }

        #endregion

        #region Construtores

        public AdvogadoPrestador()
        {

        }

        public AdvogadoPrestador(string pId, string pNome, string pOab,
            string pCpf, string pRg, DateTime pDataCadastro, Usuario pUsuario,
            Endereco pEndereco, string pEmail)
        {
            this.IdPrestador = pId;
            this.Nome = pNome;
            this.Oab = pOab;
            this.Cpf = pCpf;
            this.Rg = pRg;
            this.DataCadastro = pDataCadastro;
            this.Usuario = pUsuario;
            this.Endereco = pEndereco;
            this.Email = pEmail;
        }

        #endregion

        #region Metodos

        #region Persist
        public Boolean Persist()
        {
            ISession objSession = ConnectionFactory.getConnection().OpenSession();
            ITransaction objTransaction = objSession.BeginTransaction();

            try
            {
                objSession.Save(this);
                objTransaction.Commit();
                objSession.Close();
                return true;
            }
            catch (Exception ex)
            {
                objTransaction.Rollback();
                objSession.Close();
                throw ex;
            }
        }
        #endregion

        #region Update
        public Boolean Update()
        {
            ISession objSession = ConnectionFactory.getConnection().OpenSession();
            ITransaction objTransaction = objSession.BeginTransaction();

            try
            {
                objSession.Update(this);
                objTransaction.Commit();
                objSession.Close();
                return true;
            }
            catch (Exception ex)
            {
                objTransaction.Rollback();
                objSession.Close();
                throw ex;
            }
        }
        #endregion

        #region Delete
        public static Boolean Delete(string pIdAdvogado)
        {
            AdvogadoPrestador objAdvogado = null;
            ISession objSession = ConnectionFactory.getConnection().OpenSession();
            ITransaction objTransaction = objSession.BeginTransaction();

            try
            {
                objAdvogado = (AdvogadoPrestador)objSession.Load(typeof(Model.AdvogadoPrestador), pIdAdvogado);
                objSession.Delete(objAdvogado);
                objTransaction.Commit();
                objSession.Close();
                return true;
            }
            catch (Exception ex)
            {
                objTransaction.Rollback();
                objSession.Close();
                throw ex;
            }
        }
        #endregion

        #region RetriveObject
        public static AdvogadoPrestador RetriveObject(string pIdAdvogado)
        {
            AdvogadoPrestador objAdvogado = null;
            ISession objSession = ConnectionFactory.getConnection().OpenSession();
            ITransaction objTransaction = objSession.BeginTransaction();

            try
            {
                objAdvogado = (AdvogadoPrestador)objSession.Load(typeof(Model.AdvogadoPrestador), pIdAdvogado);
                objSession.Close();
            }
            catch (Exception ex)
            {
                objTransaction.Rollback();
                objSession.Close();
                throw ex;
            }
            return objAdvogado;
        }
        #endregion

        #region RetriveObjects
        public static IList RetriveObjects()
        {
            IList lisAdvogados;
            ISession objSession = ConnectionFactory.getConnection().OpenSession();
            ITransaction objTransaction = objSession.BeginTransaction();

            try
            {
                ICriteria criteria = objSession.CreateCriteria(typeof(Model.AdvogadoPrestador));
                criteria.AddOrder(Order.Asc("Nome"));

                lisAdvogados = criteria.List();

                objSession.Close();
            }
            catch (Exception ex)
            {
                lisAdvogados = null;
                objTransaction.Rollback();
                throw ex;
            }
            return lisAdvogados;
        }
        #endregion

        #region RetriveObjectsByNome
        public static IList RetriveObjectsByName(string pNome)
        {
            IList listAdvogados;
            ISession objSession = ConnectionFactory.getConnection().OpenSession();
            ITransaction objTransaction = objSession.BeginTransaction();

            try
            {
                ICriteria criteria = objSession.CreateCriteria(typeof(Model.AdvogadoPrestador));
                criteria.Add(Expression.Like("Nome", pNome))
                        .AddOrder(Order.Asc("Nome"));
                listAdvogados = criteria.List();
                objSession.Close();

            }
            catch (Exception ex)
            {
                listAdvogados = null;
                objTransaction.Rollback();
                throw ex;
            }

            return listAdvogados;
        }
        #endregion

        #endregion
    }
}
